home *** CD-ROM | disk | FTP | other *** search
- #include "text layer.h"
- #include "program globals.h"
- #if USE_DRAG
- #include "drag layer.h"
- #endif
- #include "window layer.h"
- #include "memory layer.h"
- #include <FixMath.h>
-
- #define RequireWindow(x) if (theWindow==0L) return x
- #define RequireTE(x) { \
- hTE=GetWindowTE(theWindow); \
- if (hTE==0L) return x; }
-
- enum { key_LeftArrow=0x1c, key_RightArrow, key_UpArrow, key_DownArrow };
-
- Boolean AnyTextInScrapQQ(void)
- {
- long dummy;
-
- LoadScrap();
- return (GetScrap(0L, 'TEXT', &dummy)!=noTypeErr);
- }
-
- void SetTheText(WindowRef theWindow, Ptr data, long count)
- {
- TEHandle hTE;
- ControlHandle vScrollBar;
-
- RequireWindow();
- RequireTE();
- vScrollBar=GetWindowVScrollBar(theWindow);
-
- TESetText(data, count, hTE);
- TESetSelect(0, 0, hTE);
- if (vScrollBar!=0L)
- AdjustVScrollBar(vScrollBar, hTE);
- #if USE_DRAG
- ResetHiliteRgn(theWindow);
- #endif
- }
-
- Boolean AnyTextQQ(WindowRef theWindow)
- {
- TEHandle hTE;
-
- RequireWindow(FALSE);
- RequireTE(FALSE);
-
- return ((**hTE).teLength!=0);
- }
-
- Boolean AnyHighlightedQQ(WindowRef theWindow)
- {
- TEHandle hTE;
-
- RequireWindow(FALSE);
- RequireTE(FALSE);
-
- return ((**hTE).selStart!=(**hTE).selEnd);
- }
-
- short SelectionStart(WindowRef theWindow)
- {
- TEHandle hTE;
-
- RequireWindow(-1);
- RequireTE(-1);
-
- return (**hTE).selStart;
- }
-
- short SelectionEnd(WindowRef theWindow)
- {
- TEHandle hTE;
-
- RequireWindow(-1);
- RequireTE(-1);
-
- return (**hTE).selEnd;
- }
-
- Boolean InsertBeforeStart(WindowRef theWindow, Str255 theStr)
- {
- TEHandle hTE;
- unsigned long len;
- unsigned long oldLen;
-
- RequireWindow(FALSE);
- RequireTE(FALSE);
-
- len=theStr[0];
- hTE=GetWindowTE(theWindow);
- oldLen=(**hTE).teLength;
- if (oldLen+len>32767)
- return FALSE;
-
- TEInsert(&theStr[1], len, hTE);
- SetWindowIsModified(theWindow, TRUE);
- #if USE_DRAG
- ResetHiliteRgn(theWindow);
- #endif
- return TRUE;
- }
-
- Boolean InsertAfterEnd(WindowRef theWindow, Str255 theStr)
- {
- TEHandle hTE;
- unsigned long len;
- unsigned long oldLen;
- unsigned long oldSelStart;
- unsigned long oldSelEnd;
-
- RequireWindow(FALSE);
- RequireTE(FALSE);
-
- len=theStr[0];
- hTE=GetWindowTE(theWindow);
- oldLen=(**hTE).teLength;
- if (oldLen+len>32767)
- return FALSE;
-
- oldSelStart=(**hTE).selStart;
- oldSelEnd=(**hTE).selEnd;
- TESetSelect(oldSelEnd, oldSelEnd, hTE);
- TEInsert(&theStr[1], len, hTE);
- TESetSelect(oldSelStart, oldSelEnd, hTE);
- SetWindowIsModified(theWindow, TRUE);
- #if USE_DRAG
- ResetHiliteRgn(theWindow);
- #endif
- return TRUE;
- }
-
- void GetSelectionString(WindowRef theWindow, Str255 theStr)
- {
- TEHandle hTE;
- short selStart, selEnd;
-
- RequireWindow();
- RequireTE();
-
- selStart=SelectionStart(theWindow);
- selEnd=SelectionEnd(theWindow);
- if (selEnd-selStart>255)
- selEnd=selStart+255;
- Mymemcpy((Ptr)&theStr[1], (Ptr)((unsigned long)(*((**hTE).hText))+selStart), selEnd-selStart);
- theStr[0]=selEnd-selStart;
- }
-
- short TotalNumberOfLines(TEHandle hTE)
- {
- short numLines;
-
- if (hTE==0L)
- return -1;
-
- numLines=(**hTE).nLines;
- if (*((unsigned char*)((long)(*((**hTE).hText))+(**hTE).teLength-1))==0x0d)
- numLines++;
-
- return numLines;
- }
- void HandleShiftArrow(TEHandle hTE, unsigned char theChar)
- {
- short offset, destOffset;
- short lineNum, lineStart;
- short offsetFromLineStart;
-
- switch (theChar)
- {
- case key_UpArrow:
- offset=(**hTE).selStart;
- lineNum=LineNumberFromOffset(hTE, offset);
- if (lineNum==0)
- destOffset=0;
- else
- {
- lineStart=LineStart(hTE, lineNum);
- offsetFromLineStart=offset-lineStart;
- if ((offset==(**hTE).teLength) && ((*((**hTE).hText))[offset-1]=='\r'))
- destOffset=LineStart(hTE, lineNum);
- else
- {
- destOffset=LineStart(hTE, lineNum-1)+offsetFromLineStart;
- if (destOffset>=lineStart)
- destOffset=lineStart-1;
- }
- }
- if (destOffset!=offset)
- TESetSelect(destOffset, (**hTE).selEnd, hTE);
- break;
- case key_DownArrow:
- offset=(**hTE).selEnd;
- lineNum=LineNumberFromOffset(hTE, offset);
- lineStart=LineStart(hTE, lineNum);
- offsetFromLineStart=offset-lineStart;
- destOffset=LineStart(hTE, lineNum+1)+offsetFromLineStart;
- if (destOffset>=(**hTE).teLength)
- destOffset=(*